﻿Option Explicit On
Option Strict On
Public Class frmMain
    Dim intCurrentI As Integer
    Dim intMaxI As Integer
    Dim intX As Integer
    Dim intY As Integer
    Dim bolRunning As Boolean
    Dim intCAWorld(0, 0) As Integer
    Dim bmpCAWorld As Bitmap
    Dim gfxCAWorld As Graphics

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
    End Sub

    Private Sub nudX_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudX.ValueChanged
        picCAWorld.Width = CInt(nudX.Value)
    End Sub

    Private Sub nudY_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudY.ValueChanged
        picCAWorld.Height = CInt(nudY.Value)
    End Sub

    Private Sub btnStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartStop.Click
        If bolRunning = False Then
            Init()
            btnStartStop.Text = "Stop"
            bolRunning = True
        Else
            btnStartStop.Text = "Start"
            bolRunning = False
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        intCurrentI += 1
        If intCurrentI >= intMaxI Or bolRunning = False Then
            Timer1.Enabled = False
            btnStartStop.Text = "Start"
        End If

        Me.ToolStripStatusLabel1.Text = intCurrentI & " of " & intMaxI
        CalcCAWorldStartState()
        DrawCAWorld()
    End Sub

    Private Sub Init()
        intCurrentI = 0
        intMaxI = CInt(nudI.Value)
        intX = CInt(nudX.Value)
        intY = CInt(nudY.Value)
        ReDim intCAWorld(intX, intY)
        CalcCAWorldStartState()
        bolRunning = True
        Timer1.Enabled = True
    End Sub

    Private Sub CalcCAWorld()

    End Sub

    Private Sub DrawCAWorld()
        Dim x As Integer
        Dim y As Integer
        Dim intXScaleFactor As Integer
        Dim intYScaleFactor As Integer
        Dim intXSize As Integer
        Dim intYSize As Integer

        intXScaleFactor = CInt(nudXScale.Value)
        intYScaleFactor = CInt(nudYScale.Value)
        intXSize = CInt(nudXSize.Value)
        intYSize = CInt(nudYSize.Value)
        bmpCAWorld = New Bitmap(intX, intY)
        gfxCAWorld = Graphics.FromImage(bmpCAWorld)

        For x = 0 To intX - 1
            For y = 0 To intY - 1
                If intCAWorld(x, y) = 0 Then
                    gfxCAWorld.FillRectangle(Brushes.White, x * intXScaleFactor, y * intYScaleFactor, intXSize, intYSize)
                    'gfxCAWorld.DrawRectangle(Pens.White, x * 10, y * 10, 10, 10)
                Else
                    gfxCAWorld.FillRectangle(Brushes.Black, x * intXScaleFactor, y * intYScaleFactor, intXSize, intYSize)
                    'gfxCAWorld.DrawRectangle(Pens.Black, x * 10, y * 10, 10, 10)
                End If

            Next

        Next
        picCAWorld.Image = bmpCAWorld
        Application.DoEvents()
    End Sub

    Private Sub CalcCAWorldStartState()
        Dim x As Integer
        Dim y As Integer
        Randomize()
        For x = 0 To intX - 1
            For y = 0 To intY - 1
                intCAWorld(x, y) = CInt(Rnd(1))
            Next
        Next
    End Sub
End Class
